package com.i2works.smartluolong.web.controller.index;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.entity.ListParamsQuery;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.plugin.Pager;
import com.i2works.smartluolong.web.entity.items.*;
import com.i2works.smartluolong.web.service.items.IItemsCategoryService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.i2works.smartluolong.web.service.items.IMainItemsService;
import com.i2works.smartluolong.web.utils.BaseModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping({"/interact"})
public class InteractController extends BaseModule
{
  private final IItemsCategoryService itemsCategoryService;
  private final IMainItemsService mainItemsService;
  private final IItemsService itemsService;

  @Autowired
  public InteractController(IItemsCategoryService itemsCategoryService, IMainItemsService mainItemsService, IItemsService itemsService)
  {
    this.itemsCategoryService = itemsCategoryService;
    this.mainItemsService = mainItemsService;
    this.itemsService = itemsService;
  }

  @GetMapping({"", "/", "/index", "/{page:[0-9]*}-{mainId:[0-9]*}"})
  public String zwgk(@ModelAttribute ListParamsQuery params, Model model)
  {
    try
    {
      params.setCateId(21);
      ItemsCategoryQuery cateQuery = new ItemsCategoryQuery();
      cateQuery.setStatus(1);
      List<ItemsCategoryDO> cateList = this.itemsCategoryService.queryList(cateQuery);
      cateQuery.setId(params.getCateId());
      ItemsCategoryDO cate = (ItemsCategoryDO)this.itemsCategoryService.query(cateQuery);
      model.addAttribute("cateId", params.getCateId());
      model.addAttribute("cateTitle", cate.getTitle());
      model.addAttribute("cateList", cateList);
      model.addAttribute("mainId", params.getMainId());

      MainItemsQuery mainQuery = new MainItemsQuery();
      mainQuery.setCateId(params.getCateId());
      mainQuery.setStatus(1);
      List<MainItemsDO> mainList = this.mainItemsService.queryList(mainQuery);
      model.addAttribute("mainList", mainList);

      //分页
      if (params.getPage() == null || params.getPage() == 0) {
        params.setPage(1);
      }
          ItemsQuery query = new ItemsQuery();
          List<ItemsDO> list = Lists.newArrayList();
          query.setCateId(params.getCateId());
          query.setStatus(1);
          if (params.getMainId() != null) {
            query.setMainId(params.getMainId());
          }
          Integer pageNumber = params.getPage();
          //查询总数量
          Integer queryCount = itemsService.queryCount(query);
      if (queryCount > 0) {
        query.setOffset(query.getLimit() * (pageNumber - 1));
        list = itemsService.queryList(query);
      }
      model.addAttribute("list", list);
      model.addAttribute("pager", new Pager(pageNumber, queryCount,10));
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    return "/interact";
  }

  @GetMapping({"/detail/{id}"})
  public String detail(@PathVariable("id") Integer id , Model model )
  {
    try
    {
      ItemsQuery query = new ItemsQuery();
      query.setId(id);
      query.setStatus(1);
      ItemsDO itemsDO = itemsService.query(query);
      model.addAttribute("itemsDO", itemsDO);
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    return "/interactDetail";
  }


}